﻿using System.Windows.Forms;

namespace Common
{
    public class ShowMsg
    {
        private static log4net.ILog logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        delegate void ShowMsgCallBack(ListBox listbox, string text, int i = 0);
        public static void showMsg(ListBox listbox, string text, int i = 0)
        {
            if (listbox.InvokeRequired)
            {                
                ShowMsgCallBack showMsgCallBack = showMsg;
                listbox.Invoke(showMsgCallBack, new object[] { listbox, text, i });
                if (listbox.Items.Count > 1000)
                    listbox.Items.Clear();
            }
            else
            {
                listbox.Items.Add(text);
                if (i == 0)
                    logger.Debug(text);
                else if (i == 1)
                    logger.Info(text);
                else if (i == 2)
                    logger.Warn(text);
                else if (i == 3)
                    logger.Error(text);
                else if (i == 4)
                    logger.Fatal(text);
                else if (i == 5)//不写日志
                { }
                listbox.SelectedIndex = listbox.Items.Count - 1;
                listbox.ClearSelected();
            }
        }
    }
}
